import 'dart:ui';
import 'package:first_flutter_project/main/utils/SizeExtension.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


const Duration _kDuration = const Duration(milliseconds: 1000);

final Size _kSize = Size(50, 50);

class LoadingIndicatorWidget2 extends StatefulWidget {
  ///loading小球区域大小
  final Size? size;

  ///动画持续时间
  final Duration duration;

  const LoadingIndicatorWidget2(
      {this.size, Key? key, this.duration = _kDuration})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _LoadingIndicatorWidgetState2();
}

class _LoadingIndicatorWidgetState2 extends State<LoadingIndicatorWidget2>
    with SingleTickerProviderStateMixin {
  AnimationController? _controller;
  Animation<double>? _leftAnimation;
  Animation<double>? _centerAnimation;
  Animation<double>? _rightAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(duration: widget.duration, vsync: this)
      ..repeat();
    _initAnimation();
  }

  void _initAnimation() {

    CurveTween curveTween = CurveTween(curve: Curves.easeInOut);
    _leftAnimation = TweenSequence<double>([
      TweenSequenceItem<double>(
          tween: Tween(begin: 0.1, end: 0.5).chain(curveTween), weight: 1),
      TweenSequenceItem<double>(
          tween: Tween(begin: 0.5, end: 0.9).chain(curveTween), weight: 1),
      TweenSequenceItem(
          tween: Tween(begin: 0.9, end: 1.0).chain(curveTween), weight: 1),
    ]).animate(_controller!);

    _centerAnimation = TweenSequence<double>([
      TweenSequenceItem<double>(
          tween: Tween(begin: 0.5, end: 0.9).chain(curveTween), weight: 1),
      TweenSequenceItem<double>(
          tween: Tween(begin: 0.9, end: 1.0).chain(curveTween), weight: 1),
      TweenSequenceItem<double>(
          tween: Tween(begin: 1.0, end: 1.4).chain(curveTween), weight: 1),
    ]).animate(_controller!);

    _rightAnimation = TweenSequence<double>([
      TweenSequenceItem<double>(
          tween: Tween(begin: 0.9, end: 1.0).chain(curveTween), weight: 1),
      TweenSequenceItem<double>(
          tween: Tween(begin: 1.0, end: 1.4).chain(curveTween), weight: 1),
      TweenSequenceItem<double>(
          tween: Tween(begin: 1.4, end: 1.8).chain(curveTween), weight: 1),
    ]).animate(_controller!);


  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        alignment: Alignment.center,
        child: AnimatedBuilder(
          animation: _controller!,
          builder: (context, child) {
            return CustomPaint(
              size: widget.size ?? _kSize,
              painter: _CircleIndicatorPainter(
                offsetValue: [
                  _leftAnimation?.value ?? 0,
                  _centerAnimation?.value ?? 0,
                  _rightAnimation?.value ?? 0
                ],
                leftColor: Color(0xffa8e1d8),
                centerColor: Color(0xff00ba9e),
                rightColor: Color(0xff60d1c0),
              ),
            );
          },
        ),
      ),
    );
  }


  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }
}

class _CircleIndicatorPainter extends CustomPainter {
  ///小圆球偏移的位置
    List<double>? offsetValue;


  ///左边小球的颜色#a8e1d8
  final Color? leftColor;

  ///右边小球的颜色
  final Color? rightColor;

  ///中间小球的颜色
  final Color? centerColor;

  late Paint _painter;


  _CircleIndicatorPainter({
    required this.offsetValue,
    this.leftColor = Colors.lightGreen,
    this.centerColor = Colors.green,
    this.rightColor = Colors.green,
  })  :super(){
    _painter = Paint()
      ..color = leftColor!
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;
    assert(offsetValue != null);
    assert(centerColor != null);
    assert(rightColor != null);

  }
  @override
  void paint(Canvas canvas, Size size) {
    ///小球的大小半径,设置为宽度的1/10
    double radius = size.width / 10;
    print("object第一个小球哈哈:${offsetValue![0]}");
    // print("object-size.width:${size.width}");
    // print("object-size.height:${size.height}");


    // canvas.drawCircle(
    //         Offset(offsetValue![0] * size.width, size.height / 2), 20, _painter);

    // double assistRadius;
    // if (offsetValue![0]>0.9) {
    //   ///动画0.9-1.0区间的缩放效果
    //   assistRadius = offsetValue![0] * size.width - size.width + radius;
    //   canvas.drawCircle(Offset(size.width - radius, size.height / 2),
    //       size.width - offsetValue![0] * size.width, _painter);
    //   //
    //   // ///同时绘制辅助小球
    //   canvas.drawCircle(
    //       Offset(radius, size.height / 2), assistRadius, _painter);
    // } else {
    //   ///动画0.1-0.9区间的平移动画
    //   canvas.drawCircle(Offset(offsetValue![0] * size.width, size.height / 2),
    //       radius, _painter);
    // }
    ///计算第一个小球的位置和半径
    ///绘制第一个小球
    canvas.save();
    double assistRadius;
    if (offsetValue![0] * size.width > size.width - radius) {
      assistRadius = offsetValue![0] * size.width - size.width + radius;
      canvas.drawCircle(Offset(size.width - radius, size.height / 2),
          size.width - offsetValue![0] * size.width, _painter);
      //
      // ///同时绘制辅助小球
      canvas.drawCircle(
          Offset(radius, size.height / 2), assistRadius, _painter);
    } else {
      canvas.drawCircle(Offset(offsetValue![0] * size.width, size.height / 2),
          radius, _painter);
    }
    canvas.restore();

    _painter..color = centerColor!;

    ///绘制第二个小球
    canvas.save();
    if (offsetValue![1] * size.width <= size.width - radius) {
      canvas.drawCircle(Offset(offsetValue![1] * size.width, size.height / 2),
          radius, _painter);
    } else if (offsetValue![1] * size.width > size.width - radius &&
        offsetValue![1] * size.width <= size.width) {
      assistRadius = offsetValue![1] * size.width - size.width + radius;
      canvas.drawCircle(Offset(size.width - radius, size.height / 2),
          size.width - offsetValue![1] * size.width, _painter);

      ///同时绘制辅助小球
      canvas.drawCircle(
          Offset(radius, size.height / 2), assistRadius, _painter);
    } else if (offsetValue![1] * size.width > size.width) {
      canvas.drawCircle(
          Offset(offsetValue![1] * size.width + radius - size.width,
              size.height / 2),
          radius,
          _painter);
    }
    canvas.restore();

    _painter..color = rightColor!;

    ///绘制第三个小球
    canvas.save();
    if (offsetValue![2] * size.width <= size.width) {
      assistRadius = offsetValue![2] * size.width - size.width + radius;
      canvas.drawCircle(Offset(size.width - radius, size.height / 2),
          size.width - offsetValue![2] * size.width, _painter);

      ///同时绘制辅助小球
      canvas.drawCircle(
          Offset(radius, size.height / 2), assistRadius, _painter);
    } else
      canvas.drawCircle(
          Offset(offsetValue![2] * size.width + radius - size.width,
              size.height / 2),
          radius,
          _painter);
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this;
  }
}

final double _kPaddingBottom = 100.w;

///
/// 加载数据为空的页面
///
// class LoadDataEmptyWidget extends StatelessWidget {
//   /// 自定义提示
//   final String? tipMessage;
//
//   /// 自定义高亮文字
//   final String? highlightText;
//
//   const LoadDataEmptyWidget({Key? key, this.tipMessage, this.highlightText})
//       : super(key: key);
//
//   @override
//   Widget build(BuildContext context) {
//     return Expanded(
//       child: Container(
//         color: LeTalkColors.c_fffafafa,
//         width: MediaQuery.of(context).size.width,
//         height: MediaQuery.of(context).size.height,
//         padding: EdgeInsets.only(bottom: _kPaddingBottom),
//         child: Column(
//           mainAxisAlignment: MainAxisAlignment.center,
//           crossAxisAlignment: CrossAxisAlignment.center,
//           children: [
//             Image.asset(
//               LeTalkImageUrl.leke_data_empty_image,
//               package: LetalkStringUrl.package_name,
//             ),
//             SizedBox(
//               height: 15.w,
//               width: MediaQuery.of(context).size.width,
//             ),
//             _textWidget(),
//           ],
//         ),
//       ),
//     );
//   }
//
//   Widget _textWidget() {
//     if (highlightText != null &&
//         tipMessage != null &&
//         tipMessage?.split(highlightText ?? "").length == 2) {
//       final textArr = tipMessage?.split(highlightText ?? "");
//       return RichText(
//         text: TextSpan(
//           style: TextStyle(
//             fontSize: LeTalkFontSize.font_16,
//           ),
//           children: <InlineSpan>[
//             TextSpan(
//                 text: textArr?[0],
//                 style: TextStyle(color: LeTalkColors.c_ff323332)),
//             TextSpan(
//                 text: highlightText,
//                 style: TextStyle(color: LeTalkColors.c_ff60d195)),
//             TextSpan(
//                 text: textArr?[1],
//                 style: TextStyle(color: LeTalkColors.c_ff323332)),
//           ],
//         ),
//       );
//     } else {
//       return Text(
//         tipMessage ?? LetalkStringUrl.empty_data,
//         style: TextStyle(
//           color: LeTalkColors.c_ff999999,
//           fontSize: LeTalkFontSize.font_14,
//         ),
//       );
//     }
//   }
// }
//
// ///
// /// 加载数据失败的页面
// ///
// class LoadDataErrorWidget extends StatelessWidget {
//   final String? errorMessage;
//
//   const LoadDataErrorWidget({Key? key, this.errorMessage}) : super(key: key);
//
//   @override
//   Widget build(BuildContext context) {
//     return Expanded(
//       child: Container(
//         color: LeTalkColors.c_fffafafa,
//         padding: EdgeInsets.only(bottom: _kPaddingBottom),
//         child: Column(
//           mainAxisAlignment: MainAxisAlignment.center,
//           crossAxisAlignment: CrossAxisAlignment.center,
//           children: [
//             Image.asset(
//               LeTalkImageUrl.leke_data_error_image,
//               package: LetalkStringUrl.package_name,
//             ),
//             SizedBox(height: 15.w, width: MediaQuery.of(context).size.width),
//             Text(
//               errorMessage ?? LetalkStringUrl.error_data,
//               style: TextStyle(
//                   color: LeTalkColors.c_ff999999,
//                   fontSize: LeTalkFontSize.font_14),
//             ),
//           ],
//         ),
//       ),
//     );
//   }
// }
//
// ///
// /// 无网络的展示界面
// ///
// class NoNetWorkWidget extends StatelessWidget {
//   final String? message;
//
//   const NoNetWorkWidget({Key? key, this.message}) : super(key: key);
//
//   @override
//   Widget build(BuildContext context) {
//     return Expanded(
//       child: Container(
//         color: LeTalkColors.c_fffafafa,
//         width: MediaQuery.of(context).size.width,
//         height: MediaQuery.of(context).size.height,
//         padding: EdgeInsets.only(bottom: _kPaddingBottom),
//         child: Column(
//           mainAxisAlignment: MainAxisAlignment.center,
//           crossAxisAlignment: CrossAxisAlignment.center,
//           children: [
//             Image.asset(
//               LeTalkImageUrl.leke_no_network_image,
//               package: LetalkStringUrl.package_name,
//             ),
//             SizedBox(
//               height: 15.w,
//               width: MediaQuery.of(context).size.width,
//             ),
//             Text(
//               message ?? LetalkStringUrl.no_network,
//               style: TextStyle(
//                   color: LeTalkColors.c_ff999999,
//                   fontSize: LeTalkFontSize.font_14),
//             ),
//           ],
//         ),
//       ),
//     );
//   }
// }
//
// ///
// /// 默认的Appbar样式 ,高度为44
// /// 带返回按钮 标题大小18sp,文字居中
// /// 需要重写，设置适配
// ///
// class LeTalkAppBar extends AppBar {
//   final String? titleText;
//   final Widget? label;
//
//   LeTalkAppBar({
//     this.titleText,
//     required BuildContext? context,
//     this.label,
//     VoidCallback? backPressed,
//   })  : assert(context != null),
//         assert(titleText != null || label != null),
//         super(
//         brightness: Brightness.light,
//         toolbarHeight: 44.w,
//         title: label != null
//             ? label
//             : Text(
//           titleText ?? "",
//           style: TextStyle(
//             color: LeTalkColors.c_ff333333,
//             fontSize: LeTalkFontSize.font_18,
//             fontWeight: FontWeight.bold,
//           ),
//         ),
//         backgroundColor: Colors.white,
//         elevation: 0,
//         centerTitle: true,
//         leading: IconButton(
//           constraints: BoxConstraints.tightFor(width: 24.w, height: 24.w),
//           iconSize: 24.w,
//           icon: Icon(
//             LeTalkIcons.back_arrow,
//             color: LeTalkColors.c_ff333333,
//           ),
//           onPressed: backPressed != null
//               ? backPressed
//               : () => Navigator.of(context!).pop(),
//         ),
//       );
//
//   ///不带返回键的，文字居中
//   LeTalkAppBar.main({
//     this.titleText,
//     this.label,
//   })  : assert(titleText != null || label != null),
//         super(
//         automaticallyImplyLeading: false,
//         brightness: Brightness.light,
//         toolbarHeight: 44.w,
//         title: label != null
//             ? label
//             : Text(
//           titleText ?? "",
//           style: TextStyle(
//             color: LeTalkColors.c_ff333333,
//             fontSize: LeTalkFontSize.font_18,
//           ),
//         ),
//         backgroundColor: Colors.white,
//         elevation: 0,
//         centerTitle: true,
//       );
//
//   ///不带返回键，文字居左,粗体，大小20
//   LeTalkAppBar.left({
//     this.titleText,
//     this.label,
//     List<Widget>? actions,
//   })  : assert(titleText != null || label != null),
//         super(
//         automaticallyImplyLeading: false,
//         brightness: Brightness.light,
//         toolbarHeight: 44.w,
//         title: label != null
//             ? label
//             : Text(
//           titleText ?? "",
//           style: TextStyle(
//             color: LeTalkColors.c_ff323332,
//             fontSize: LeTalkFontSize.font_20,
//             fontWeight: FontWeight.bold,
//           ),
//         ),
//         backgroundColor: Colors.white,
//         elevation: 0,
//         actions: actions,
//         centerTitle: false,
//       );
//
//   ///自定义返回键样式
//   LeTalkAppBar.lead({
//     this.titleText,
//     required Widget? leading,
//     this.label,
//     double? leadingWidth,
//     List<Widget>? actions,
//   })  : assert(titleText != null || label != null),
//         assert(leading != null),
//         super(
//           brightness: Brightness.light,
//           toolbarHeight: 44.w,
//           title: label != null
//               ? label
//               : Text(
//             titleText ?? "",
//             style: TextStyle(
//               color: LeTalkColors.c_ff333333,
//               fontSize: LeTalkFontSize.font_18,
//             ),
//           ),
//           backgroundColor: Colors.white,
//           elevation: 0,
//           centerTitle: true,
//           leading: leading,
//           leadingWidth: leadingWidth,
//           actions: actions);
// }
//
// ///
// /// 默认的Appbar样式 ,高度为44
// /// 带返回按钮 标题大小18sp,文字居中
// /// 需要重写，设置适配
// ///
// class ModalAppBar extends AppBar {
//   final String? titleText;
//   final Widget? label;
//
//   ModalAppBar({
//     this.titleText,
//     required BuildContext context,
//     this.label,
//     VoidCallback? leftTapBack,
//     VoidCallback? rightTapBack,
//   })  : assert(titleText != null || label != null),
//         super(
//         brightness: Brightness.light,
//         toolbarHeight: 44.w,
//         titleSpacing: 0,
//         title: Row(
//           mainAxisAlignment: MainAxisAlignment.spaceBetween,
//           children: [
//             CupertinoButton(
//               child: Text(
//                 "取消",
//                 style: TextStyle(
//                   // height: 16.w,
//                   color: LeTalkColors.c_ff333333,
//                   fontSize: LeTalkFontSize.font_16,
//                 ),
//                 // ),
//               ),
//               onPressed: leftTapBack != null
//                   ? leftTapBack
//                   : () => Navigator.of(context).pop(),
//             ),
//             label != null
//                 ? label
//                 : Text(
//               titleText ?? "",
//               style: TextStyle(
//                 color: LeTalkColors.c_ff333333,
//                 fontSize: LeTalkFontSize.font_18,
//               ),
//             ),
//             CupertinoButton(
//               child: Text(
//                 "多选",
//                 style: TextStyle(
//                   color: LeTalkColors.c_ff333333,
//                   fontSize: LeTalkFontSize.font_16,
//                 ),
//               ),
//               onPressed: null,
//               // rightTapBack != null
//               //     ? rightTapBack
//               //     : () => Navigator.of(context).pop(),
//             ),
//           ],
//         ),
//         backgroundColor: Colors.white,
//         elevation: 0,
//         centerTitle: true,
//         leadingWidth: 0,
//         automaticallyImplyLeading: false,
//       );
// }
